//
//  KLSGMainViewController.m
//  KaoLaShiGuangFM
//
//  Created by lanou3g on 15/10/27.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "KLSGMainViewController.h"
#import "KLSGPlayRadioViewController.h"
#import "AppDelegate.h"
#import "KLSGTabBar.h"

@interface KLSGMainViewController ()

@end

@implementation KLSGMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // 添加“首页”
    self.recommendVC = [[KLSGRecommendViewController alloc] init];
    [self addChildVc:_recommendVC title:@"首页" image:@"shouye" selectImage:@"shouye"];
    
    // 添加分类
    self.categoryVC = [[KLSGCategoryViewController alloc] init];
    [self addChildVc:_categoryVC title:@"分类" image:@"fenlei" selectImage:@"fenlei"];
    
    // 添加直播
    self.liveWebVC = [[KLSGLiveWebViewController alloc] init];
    [self addChildVc:_liveWebVC title:@"直播" image:@"zhibo" selectImage:@"zhibo"];
    
    // 添加排行榜
    self.rankingListVC = [[KLSGRankingListViewController alloc] init];
    [self addChildVc:_rankingListVC title:@"排行榜" image:@"paihang" selectImage:@"paihang"];
    
    // 更换系统自带的tabbar
    [self setValue:[[KLSGTabBar alloc] init] forKeyPath:@"tabBar"];
    
//    self.playView = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth * 2 / 5 - 2, kScreenHeight - 60, kScreenWidth / 5, 120)];
//    self.playView.backgroundColor = [UIColor whiteColor];
//    self.playView.layer.cornerRadius = kScreenWidth / 10;
//    self.playView.layer.masksToBounds = YES;
//    [self.view addSubview:self.playView];
    
    
    
    // 添加播放按钮
    self.playBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.playBtn.frame = CGRectMake(0, 0, kScreenWidth / 5 - 16, kScreenWidth / 5 - 16);
    self.playBtn.center = CGPointMake(kScreenWidth / 2, kScreenHeight - (kScreenWidth / 10));
    self.playBtn.layer.cornerRadius = kScreenWidth / 10 - 8;
    self.playBtn.layer.masksToBounds = YES;

    [self.playBtn setBackgroundImage:[UIImage imageNamed:@"place"] forState:(UIControlStateNormal)];

    self.playBtn.backgroundColor = [UIColor clearColor];//[UIColor colorWithRed:234 / 256.0 green:79 / 256.0 blue:56 / 256.0 alpha:1];
    [self.playBtn addTarget:self action:@selector(playVC:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:self.playBtn];
    
    self.picture = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth / 5, kScreenWidth / 5)];
    self.picture.alpha = 1;
    self.picture.center = CGPointMake(kScreenWidth / 2, kScreenHeight - (kScreenWidth / 10));
    self.picture.image = [UIImage imageNamed:@"play_icon_new"];
    //    self.picture.userInteractionEnabled = YES;
    [self.view addSubview:self.picture];
    
    self.icon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth / 5 - 16, kScreenWidth / 5 - 16)];
    self.icon.center = CGPointMake(kScreenWidth / 2, kScreenHeight - (kScreenWidth / 10));
    self.icon.layer.cornerRadius = kScreenWidth / 10 - 8;
    self.icon.layer.masksToBounds = YES;
    self.icon.hidden = YES;
    [self.view addSubview:self.icon];

}

/**
 * 添加一个子控制器
 *
 * @param childVc  自控制器
 * @param title 标题
 * @param image 图片
 * @param selectImage 选中的图片
 */

- (void)addChildVc:(UIViewController *)childVC title:(NSString *)title image:(NSString *)image selectImage:(NSString *)selectImage
{
    // 设置子控制器的标题和图片
    childVC.title = title; // 同时设置tabbar和navigationBar的文字
    childVC.tabBarItem.image = [UIImage imageNamed:image];
    // 这张图片按照原始图片显示出来，不要自动选染成其他颜色
    childVC.tabBarItem.selectedImage = [[UIImage imageNamed:selectImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 设置文字样式
    NSMutableDictionary *textArray = [NSMutableDictionary dictionary];
    textArray[NSForegroundColorAttributeName] = KLSGColor(123, 123, 123);
    [childVC.tabBarItem setTitleTextAttributes:textArray forState:UIControlStateNormal];
    
    NSMutableDictionary *selectTextArray = [NSMutableDictionary dictionary];
    selectTextArray[NSForegroundColorAttributeName] = [UIColor colorWithRed:234 / 256.0 green:79 / 256.0 blue:56 / 256.0 alpha:1];
    [childVC.tabBarItem setTitleTextAttributes:selectTextArray forState:UIControlStateSelected];
    childVC.view.backgroundColor = KLSGRandomColor;
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:childVC];
    
    [self addChildViewController:nav];
}

#pragma mark -播放按钮
- (void)playVC:(UIButton *)sender
{
    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    if (appDelegate.playVC.radioId == nil) {
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请选择节目" preferredStyle:(UIAlertControllerStyleAlert)];
            [self performSelector:@selector(closeAlertC) withObject:nil afterDelay:1];
            [self presentViewController:alertC animated:YES completion:nil];
    } else {
//        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
//        appDelegate.playVC.radioId = [user objectForKey:@"radioId"];
//        appDelegate.playVC.index = [user integerForKey:@"index"];
//        appDelegate.playVC.radioModelArray = [user objectForKey:@"radioModelArray"];
//        appDelegate.playVC.rname = [user objectForKey:@"rname"];
        [self presentViewController:appDelegate.playVC animated:YES completion:nil];
    }

}

- (void)closeAlertC
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
